package cn.rongdao.jrkabao.ui.main.mainfragment.personal;


import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.http.request.event.CompulsoryDownLineEvent;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import cn.mifengkong.appcommon.util.ActivityStartUtil;
import cn.rongdao.jrkabao.BuildConfig;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseViewPagerFragment;
import cn.rongdao.jrkabao.bean.main.personal.PersonItemInfoBean;
import cn.rongdao.jrkabao.model.main.personal.PersonItemInfoModel;
import cn.rongdao.jrkabao.ui.cashadvance.event.CashAdvanceSuccessEvent;
import cn.rongdao.jrkabao.ui.cashadvance.event.ChangeCreditCardEvent;
import cn.rongdao.jrkabao.ui.cashadvance.event.ChangeDepositCardEvent;
import cn.rongdao.jrkabao.ui.deposit.event.DepositSuccessEvent;
import cn.rongdao.jrkabao.ui.main.mainfragment.adapter.PersonalAdapter;
import cn.rongdao.jrkabao.ui.main.mainfragment.adapter.PersonalViewHolder;
import cn.rongdao.jrkabao.ui.main.mainfragment.uicomponent.PersonUserInfoUIComponent;
import cn.rongdao.jrkabao.ui.main.mainfragment.vip.MemberPaySuccessEvent;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.LoginUtil;
import cn.rongdao.jrkabao.util.growingio.BuriedUtil;

/**
 * Created by jack on 17/5/9
 * 我的页面
 */

public class FragmentPersonal extends BaseViewPagerFragment implements FRBaseRecyclerAdapter.OnItemClickListener<PersonalViewHolder> {

    @BindView(R.id.fp_ptr_framelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;
    @BindView(R.id.fp_ll_user_info)
    PersonUserInfoUIComponent personUserInfoUIComponent;

    private PersonalAdapter personalItemInfoAdapter;

    @Override
    protected int layoutRes() {
        return R.layout.fragment_personal;
    }

    @Override
    protected void onViewReallyCreated(View view) {
        super.onViewReallyCreated(view);
        BuriedUtil.kamao_yemian_jiazaiwancheng_apply(getContext(), 4);
        EventBus.getDefault().register(this);
        personUserInfoUIComponent.setPersonal(true);
        personalItemInfoAdapter = new PersonalAdapter(getContext(), this);
        ptrRecyclerViewUIComponent.setBackground(R.color.layout_bg);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(getContext()));
        ptrRecyclerViewUIComponent.setAdapter(personalItemInfoAdapter);
        initListener();
    }

    @Override
    protected void lazyLoad() {
        ptrRecyclerViewUIComponent.delayRefresh();
    }

    private void initListener() {
        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                personUserInfoUIComponent.getUserInfo(FragmentPersonal.this);
                getItemInfo();
            }
        });
    }

    private void itemClick(PersonItemInfoBean personItemInfoBean) {
        if (CommonUtil.isStatusSuccess(personItemInfoBean.getNeed_login()) && !LoginUtil.isLogin(getContext())) {
            return;
        }
        if (!FRString.isEmpty(personItemInfoBean.getConfig_en())) {
            buriedPoint(personItemInfoBean.getConfig_en());
        }
        String url = personItemInfoBean.getUrl();
        if (!FRString.isEmpty(url)) {
            if (url.startsWith("kamao://")) {
                ActivityStartUtil.launch(getContext(), BuildConfig.APPLICATION_ID, url);
            } else {
                WebViewActivity.start(getContext(), url);
            }
        }
    }

    private void buriedPoint(String configEn) {
        switch (configEn) {
            case "cash_record":
                BuriedUtil.kamao_wode_anniu_apply(getContext(), 3);
                break;
            case "my_card_package":
                BuriedUtil.kamao_wode_anniu_apply(getContext(), 4);
                break;
            case "my_loan_record":
                BuriedUtil.kamao_wode_anniu_apply(getContext(), 5);
                break;
            case "redeem_code":
                BuriedUtil.kamao_wode_anniu_apply(getContext(), 0);
                break;
            case "inviting_friends":
                BuriedUtil.kamao_wode_anniu_apply(getContext(), 1);
                break;
            case "help_center":
                BuriedUtil.kamao_wode_anniu_apply(getContext(), 2);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void compulsoryDownLineMessage(CompulsoryDownLineEvent compulsoryDownLineEvent) {
        personUserInfoUIComponent.getUserInfo(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void changeCreditCardEventMessage(ChangeCreditCardEvent changeCreditCardEvent) {
        personUserInfoUIComponent.getUserInfo(this);
        getItemInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void changeDepositCardEventMessage(ChangeDepositCardEvent changeDepositCardEvent) {
        personUserInfoUIComponent.getUserInfo(this);
        getItemInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void depositSuccessEventMessage(DepositSuccessEvent depositSuccessEvent) {
        personUserInfoUIComponent.getUserInfo(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void uploadAvatarSuccessEventMessage(UploadAvatarSuccessEvent uploadAvatarSuccessEvent) {
        personUserInfoUIComponent.getUserInfo(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void memberPaySuccessEventMessage(MemberPaySuccessEvent memberPaySuccessEvent) {
        personUserInfoUIComponent.getUserInfo(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void cashAdvanceSuccessEventMessage(CashAdvanceSuccessEvent cashAdvanceSuccessEvent) {
        personUserInfoUIComponent.getUserInfo(this);
    }

    private void getItemInfo() {
        PersonItemInfoModel.getInstance().execute(new FRBaseObserver<ArrayList<PersonItemInfoBean>>(this) {
            @Override
            protected void onBaseNext(ArrayList<PersonItemInfoBean> data) {
                if (data.size() > 0) {
                    PersonItemInfoBean personItemInfoBean = data.get(data.size() - 1);
                    personItemInfoBean.setIs_last_item(true);
                    data.set(data.size() - 1, personItemInfoBean);
                    personalItemInfoAdapter.setDataList(data);
                }
                ptrRecyclerViewUIComponent.refreshComplete();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                ptrRecyclerViewUIComponent.refreshComplete();
            }
        });
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onItemClick(PersonalViewHolder personalViewHolder, int position) {
        itemClick(personalItemInfoAdapter.getDataList().get(position));
    }
}
